import md5 from "js-md5";
/**
 * 回到顶部
 */
export const scrollTop = (_window) => {
    //设置定时器
    (function smoothscroll() {
        var currentScroll =
            document.documentElement.scrollTop || document.body.scrollTop;
        if (currentScroll > 0) {
            window.requestAnimationFrame(smoothscroll);
            window.scrollTo(0, currentScroll - currentScroll / 5);
        }
    })();
};

/**
 * 处理页面分页总页数
 */
export const extendPageTotal = (_number, _count) => {
    return Math.ceil(Number(_number ? Number(_number / _count) : 1));
};

//判断是否是移动端
export const isMobile = () => {
    if (
        navigator.userAgent.match(
            /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
        )
    ) {
        return "wap";
    } else {
        return "web";
    }
};

//处理菜单
//处理菜单分级
export const extendsMenus = (_array) => {
    // let newArray = [];
    // Object.values(_array).forEach(i => {
    //     if (i.class_level == 0) {
    //         i.children = [];
    //         newArray.push(i);
    //         Object.values(_array).forEach(j => {
    //             if (j.class_parent == i.id) {
    //                 i.children.push(j);
    //             }
    //         })
    //     }
    // })
    // return newArray;
    let temp = {};
    let tree = [];
    _array.forEach((ele) => {
        ele.value = ele.id;
        ele.label = ele.class_title;
        temp[ele.id] = ele;
    });
    Object.values(temp).forEach((ele) => {
        if (ele.class_parent) {
            if (!temp[ele.class_parent].children) {
                temp[ele.class_parent].children = [];
            }
            temp[ele.class_parent].children.push(ele);
        } else {
            tree.push(ele);
        }
    });
    return tree;
};

//处理商品分类无限级
export const extendsStoreClass = (_array) => {
    let newArray = [];
    _array.forEach((i) => {
        if (i.classes_level == 0) {
            i.children = [];
            newArray.push(i);
            _array.forEach((j) => {
                if (j.classes_parent == i.id) {
                    i.children.push(j);
                }
            });
        }
    });
    return newArray;
};

//处理请求加密机制
export const extendsEncrypte = (
    _array,
    _axios,
    _time = "",
    arr = [],
    _res = ""
) => {
    _time = extendsTimes();
    Object.keys(_array).forEach((key) => {
        arr.push(key.toLowerCase().replace(/( |^)[a-z]/g, (L) => L.toUpperCase()));
    });
    arr = [
        ...arr.sort(function (item1, item2) {
            return item1.localeCompare(item2);
        }),
    ];
    arr.forEach((ele) => {
        _res += ele;
    });
    _axios.defaults.headers.common["Encrypted-Code"] = md5(_time + _res + _time + "Dias Software Inc.");
    _axios.defaults.headers.common["Time-Rubbing"] = _time;
    _axios.defaults.headers.common["Authorzation-Code"] = process.browser?window.localStorage.getItem('access'):'';
};

//转换请求参数
export const extendsParams = (_array) => {
    let params = new URLSearchParams();
    Object.keys(_array).forEach((key) => {
        params.append(key, _array[key]);
    });
    return params;
};

//生成时间搓
export const extendsTimes = () => {
    return new Date().getTime() + 64 * 49 * 1000;
};

//禁止放大缩小页面
export const noScale = ()=>{
    document.addEventListener('touchstart',function (event) {  
        if(event.touches.length>1){  
            event.preventDefault();  
        }  
    })  
    var lastTouchEnd=0;  
    document.addEventListener('touchend',function (event) {  
        var now=(new Date()).getTime();  
        if(now-lastTouchEnd<=300){  
            event.preventDefault();  
        }  
        lastTouchEnd=now;  
    },false)  
}

//图片文件转为工具函数
export const fileToBase64 = (img, callback)=>{
    const reader = new FileReader()
    reader.addEventListener('load', () => callback(reader.result))
    reader.readAsDataURL(img)
}

//blob转为file
export const blobToFile = (data)=>{
    let file = new File([data], new Date().getTime()+'.jpeg', {type: 'image/jpeg', lastModified: Date.now()});
    return file;
}

//三元九运
export const yuanYunData = {
    '上元一运':['一白','贪狼','水','坎','1864—1883','water'],
    '上元二运':['二黑','巨门','土','坤','1884—1903','soil'],
    '上元三运':['三碧','禄存','木','震','1904—1923','tree'],
    '中元四运':['四绿','文曲','木','巽','1924—1943','tree'],
    '中元五运':['五黄','廉贞','土','中宫','1944—1963','soil'],
    '中元六运':['六白','武曲','金','乾','1964—1983','metals'],
    '下元七运':['七赤','破军','金','兑','1984—2004','metals'],
    '下元八运':['八白','左辅','土','艮','2004—2023','soil'],
    '下元九运':['九紫','右弼','火','离','2024—2043','fire'],
}